package com.visionet.school.app.web.web.wechat;

import com.alibaba.fastjson.JSONObject;
import com.visionet.school.app.web.common.web.WebContextFactory;
import com.visionet.school.app.web.entity.member.vo.AsAndMeVo;
import com.visionet.school.app.web.entity.member.vo.AssessVo;
import com.visionet.school.app.web.entity.user.User;
import com.visionet.school.app.web.entity.vo.LoginAccountVo;
import com.visionet.school.app.web.entity.vo.MemberVo;
import com.visionet.school.app.web.entity.vo.StudentVo;
import com.visionet.school.app.web.service.file.FileClassService;
import com.visionet.school.app.web.service.member.MemberRelService;
import com.visionet.school.app.web.service.member.MemberService;
import com.visionet.school.app.web.service.user.UserService;
import com.visionet.school.app.web.service.wechat.WechatService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by hxpeng on 2017/5/11.
 */
@Controller("wechatMember")
@RequestMapping("/wechat/member")
public class MemberController {

    private static final Logger logger = LoggerFactory.getLogger(MemberController.class);

    @Autowired
    private MemberService memberService;

    @Autowired
    private FileClassService fileClassService;

    @Autowired
    private MemberRelService memberRelService;

    @Autowired
    private UserService userService;
    @Autowired
    private WechatService wechatService;


    @RequestMapping(value = "/createParent", method = RequestMethod.POST)
    public ResponseEntity<?> createParent(@RequestBody MemberVo memberVo){
        if(memberVo == null || StringUtils.isBlank(memberVo.getMemberName()) || StringUtils.isBlank(memberVo.getMemberPhone())
                || StringUtils.isBlank(memberVo.getMemberRelType())){
            return new ResponseEntity(null , HttpStatus.BAD_REQUEST);
        }
        HashMap<String,Object> map = new HashMap<>();
        try {
            LoginAccountVo loginAccountVo = memberService.createParent(memberVo);
            map.put("stat","success");
            map.put("loginAccountVo",loginAccountVo);
        }catch (Exception e){
            map.put("stat","err");
            map.put("msg",e.getMessage());
        }
        return new ResponseEntity(map , HttpStatus.OK);
    }


    @RequestMapping(value = "/studentBindParent", method = RequestMethod.POST)
    public ResponseEntity<?> studentBindParent(@RequestBody MemberVo memberVo){
        if(memberVo == null || StringUtils.isBlank(memberVo.getMemberId())){
            return new ResponseEntity(null , HttpStatus.BAD_REQUEST);
        }
        HashMap<String,Object> map = new HashMap<>();
        try {
            memberService.studentBindParent(memberVo.getMemberId());
            map.put("stat","success");

            LoginAccountVo loginAccountVo = WebContextFactory.getWebContext().getLoginAccountVo();
            User user = userService.findOneByOpenId(loginAccountVo.getOpenId());
            if(user == null){
                logger.error("未找到登陆账号");
                throw new RuntimeException("创建失败！");
            }
            loginAccountVo = wechatService.getLoginAccountVo(user);
            WebContextFactory.getWebContext().setLoginAccountVo(loginAccountVo);
            map.put("loginAccountVo",loginAccountVo);
        }catch (Exception e){
            map.put("stat","err");
            map.put("msg",e.getMessage());
        }
        return new ResponseEntity(map , HttpStatus.OK);
    }

    @RequestMapping(value = "/getMemberList", method = RequestMethod.POST)
    public ResponseEntity<?> getMemberList(HttpServletRequest request) {
        HashMap<String, Object> map = new HashMap<>();
        try {
            LoginAccountVo loginAccountVo = (LoginAccountVo) request.getSession().getAttribute("LoginAccountVo");
            //return memberService.getMemberList(loginAccountVo.getClassId());
            map.put("memberList", memberService.getMemberList("16BFD02EB985735E351C6B89A19CA3EE"));
            logger.info(map.get("memberList")+"============================================");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResponseEntity(map, HttpStatus.OK);
    }

//    @RequestMapping(value = "/getStudentName", method =  RequestMethod.POST)
//    public ResponseEntity<?> getStudentName(@RequestBody JSONObject jsonObject) {
//        String searchName = jsonObject.getString("searchName");
//        HashMap<String, Object> map = new HashMap<>();
//        try {
//            map.put("studentName", memberService.getMemberList("16BFD02EB985735E351C6B89A19CA3EE",searchName));
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return new ResponseEntity(map, HttpStatus.OK);
//    }

    @RequestMapping(value = "/getAssessById", method = RequestMethod.POST)
    public ResponseEntity<?> getAssessById(@RequestBody JSONObject jsonObject) {
        String id = jsonObject.getString("memberId");
        HashMap<String, Object> map = new HashMap<>();
        try {
            map.put("assess",memberService.getAssessById(id));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResponseEntity(map, HttpStatus.OK);
    }

    @RequestMapping(value = "/findAssess", method = RequestMethod.POST)
    public ResponseEntity<?> findAssess(HttpServletRequest request) {
        HashMap<String, Object> map = new HashMap<>();
        try {
            LoginAccountVo loginAccountVo = WebContextFactory.getWebContext().getLoginAccountVo();
            if (loginAccountVo.getMemberType().equals("1")) {
                map.put("assess",memberService.findAssess(loginAccountVo.getClassId()));
            } else {
                map.put("assess", "2");//2代表是家长，直接进入孩子统计页面
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResponseEntity(map, HttpStatus.OK);
    }

    @RequestMapping(value = "/saveAssess", method = RequestMethod.POST)
    public ResponseEntity<?> saveAssess(@RequestBody AssessVo assessVo) {
        HashMap<String, Object> map = new HashMap<>();
        try {
            map.put("msg",memberService.saveAssess(assessVo));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResponseEntity(map, HttpStatus.OK);
    }

    @RequestMapping(value = "/findAssessReport", method = RequestMethod.POST)
    public ResponseEntity<?> findAssessReport(@RequestBody AssessVo assessVo) {
        HashMap<String, Object> map = new HashMap<>();
        try {
            Map<String, Object> report = memberService.findAssessReport(assessVo.getMemberId());
            if (report.size() == 0) {
                map.put("report","null");
            } else {
                map.put("report",report);
            }
        } catch (Exception e) {
            e.printStackTrace();

        }
        return new ResponseEntity(map, HttpStatus.OK);
    }

    @RequestMapping(value = "/findReportSlide", method = RequestMethod.POST)
    public ResponseEntity<?> findReportSlide(@RequestBody AssessVo assessVo) {
        System.out.println(assessVo.getMemberId()+":"+assessVo.getStartDateSize()+":"+assessVo.getEndDateSize());
        HashMap<String, Object> map = new HashMap<>();
        try {
            Map<String, Object> date = memberService.loopDate(assessVo.getMemberId(), assessVo.getStartDateSize(), assessVo.getEndDateSize());
            if (date.size() == 0) {
                map.put("report", "null");
            } else {
                map.put("report", date);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResponseEntity(map, HttpStatus.OK);
    }

    @RequestMapping(value = "/findReportMonthYear")
    public ResponseEntity<?> findReportMonthYear(@RequestBody AssessVo assessVo) {
        HashMap<String, Object> map = new HashMap<>();
        try {
            Map<String, Object> objectMap = memberService.findReportMonthYear(assessVo.getMemberId(), assessVo.getDataType(), assessVo.getStartDateSize(), assessVo.getEndDateSize());
            if (objectMap.size() == 0) {
                map.put("report","null");
            } else {
                map.put("report", objectMap);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResponseEntity(map, HttpStatus.OK);
    }

    @RequestMapping(value = "/saveAssessTwo")
    private ResponseEntity<?> saveAssessTwo(@RequestBody AsAndMeVo asAndMeVo) {
        HashMap<String, Object> map = new HashMap<>();
        try {
            map.put("msg",memberService.saveAssessTwo(asAndMeVo));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResponseEntity(map, HttpStatus.OK);
    }

    @RequestMapping(value = "/findMemberId")
    public ResponseEntity<?> findMemberId() {
        HashMap<String, Object> map = new HashMap<>();
        try {
            LoginAccountVo loginAccountVo = WebContextFactory.getWebContext().getLoginAccountVo();
            map.put("memberIds", loginAccountVo.getStudentVoList());
            String ids = null;
            for (StudentVo vo : loginAccountVo.getStudentVoList()) {
                ids = vo.getMemberId();
            }
            Map<String, Object> report = memberService.findAssessReport(ids);
            map.put("report", report);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResponseEntity(map, HttpStatus.OK);
    }
}
